package pswvclass.pswvandroid2.com;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import pswvclass.pswvandroid2.R;
import pswvclass.pswvandroid2.com.user.SafeSet;
import pswvclass.pswvandroid2.com.util.DBHelper;
import pswvclass.pswvandroid2.com.util.HttpUtils;

/**
 * Created by Administrator on 2018/12/18.
 */
public class Register extends AppCompatActivity implements View.OnClickListener {

    private String tag = "ForgetPassword";
    private int userId;
    private TextView user_account;
    private TextView user_password;
    private Button register_user;
    private DBHelper db;
    private SQLiteDatabase sd;
    protected static final int REGISTER_SUCCESS = 0;
    protected static final int ERROR = 1;
    protected static final int LOGIN_SUCCESS = 2;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.register);
        user_account = (TextView) findViewById(R.id.user_account);
        user_password = (TextView) findViewById(R.id.user_password);
        register_user = (Button) findViewById(R.id.register_user);
        register_user.setOnClickListener(this);
    }


    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case REGISTER_SUCCESS:
                    final String account = user_account.getText().toString().trim();
                    final String password = user_password.getText().toString().trim();
                    new Thread() {
                        public void run() {
                            try {
                                HttpUtils hu = new HttpUtils();
                                String data2;
                                final String src = getResources().getString(R.string.src_mysql) + "pswv/api/loginAction/login";
                                final Map<String, String> map1 = new HashMap<String, String>();
                                map1.put("account", account);
                                map1.put("password", password);
                                data2 = hu.submitPostData(src, map1, "utf-8");
                                JSONObject jsonObj = new JSONObject(data2);
                                userId = jsonObj.getJSONObject("response").getInt("userId");
                                db = new DBHelper(Register.this);
                                sd = db.getWritableDatabase();
                                int result = Integer.parseInt(jsonObj.getString("code"));
                                if (result == 0) {
                                    //继续修改
                                    Cursor cursor1 = sd.query("user", null, "userId=?", new String[]{userId + ""}, null, null, null);
                                    if (cursor1.getCount() != 0) {
                                        ContentValues values1 = new ContentValues();
                                        values1.put(DBHelper.STATE, 1);
                                        sd.update("user", values1, "userId=?", new String[]{userId + ""});
                                        Message msg = Message.obtain();
                                        msg.what = LOGIN_SUCCESS;
                                        mHandler.sendMessage(msg);
                                    } else {
                                        ContentValues values = new ContentValues();
                                        values.put(DBHelper.USERID, userId);
                                        values.put(DBHelper.ACCOUNT, account);
                                        values.put(DBHelper.PASSWORD, password);
                                        values.put(DBHelper.STATE, 1);
                                        sd.insert(DBHelper.TABLE_NAME, null, values);
                                        Message msg = Message.obtain();
                                        msg.what = LOGIN_SUCCESS;
                                        mHandler.sendMessage(msg);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                                Message msg = Message.obtain();
                                msg.what = ERROR;
                                mHandler.sendMessage(msg);
                                Log.v(tag, e + "");
                            }
                        }
                    }.start();
                    break;
                case LOGIN_SUCCESS:
                    Intent intent = new Intent();
                    intent.setClass(Register.this, SafeSet.class);
                    startActivity(intent);
                    break;
                case ERROR:
                    break;
                default:
                    break;
            }

        }

        ;

    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_user:
                final String account = user_account.getText().toString().trim();
                final String password = user_password.getText().toString().trim();

                if (TextUtils.isEmpty(account) || TextUtils.isEmpty(password)) {
                    Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                new Thread() {
                    public void run() {
                        try {
                            HttpUtils hu = new HttpUtils();
                            String data2;
                            final String src = getResources().getString(R.string.src_mysql) + "pswv/api/userAction/add";
                            final Map<String, String> map1 = new HashMap<String, String>();
                            map1.put("account", account);
                            map1.put("password", password);
                            data2 = hu.submitPostData(src, map1, "utf-8");
                            JSONObject jsonObj = new JSONObject(data2);
                            int result = Integer.parseInt(jsonObj.getString("code"));
                            if (result == 0) {
                                Message msg = Message.obtain();
                                msg.what = REGISTER_SUCCESS;
                                mHandler.sendMessage(msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            Message msg = Message.obtain();
                            msg.what = ERROR;
                            mHandler.sendMessage(msg);
                            Log.v(tag, e + "");
                        }
                    }
                }.start();
                break;
            default:
                break;
        }
    }
}
